package com.xt.mes.config.security;

import com.alibaba.fastjson.JSON;
import com.xt.mes.common.Response;
import com.xt.mes.common.ResultCodeEnum;
import com.xt.mes.util.WebUtils;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 授权失败 403
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        Response result = new Response(ResultCodeEnum.UnAuth);
        String json = JSON.toJSONString(result);
        // 将字符串渲染到客户端
        WebUtils.renderString(response, json);
    }
}
